package com.lagou.service.impl;


import com.lagou.service.WebServerFactory;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class MyTomcat implements WebServerFactory {
    public void createServer() throws Exception {

        /**
         * new 一个 Tomcat
         */
        Tomcat tomcatServer = new Tomcat();

        // 配置端口
        tomcatServer.setPort(9090);

        StandardContext ctx = (StandardContext) tomcatServer.addWebapp("/", new File("src/main").getAbsolutePath());
        ctx.setReloadable(false);
        WebResourceRoot resources = new StandardRoot(ctx);

        File additionWebInfClasses = new File("target/classes");
        //告诉Tomcat项目的class目录，根目录
        resources.addPreResources(
                new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/"));
        //启动容器
        tomcatServer.start();
        tomcatServer.getServer().await();
    }
}
